package com.raindrop.rd;

import javax.script.*;
import java.io.Reader;

/**
 * RD语言引擎，用来执行编译后的脚本
 *
 * @author BillDowney
 * @date 2021/3/22 14:44
 */
public class RDScriptEngine extends AbstractScriptEngine implements ScriptEngine {

    /**
     * 引擎工厂
     */
    private final RDScriptEngineFactory factory;

    public RDScriptEngine(RDScriptEngineFactory factory) {
        this.factory = factory;
    }

    @Override
    public Object eval(String script, ScriptContext context) throws ScriptException {
        // 定义编译器
        RDCompilable compilable = new RDCompilable(this);
        // 编译完成的对象
        CompiledScript compiledScript = compilable.compile(script);
        return compiledScript.eval(context);
    }

    @Override
    public Object eval(Reader reader, ScriptContext context) throws ScriptException {
        return this.eval(new RDCompilable(this).readString(reader), context);
    }

    @Override
    public Bindings createBindings() {
        return new SimpleBindings();
    }

    @Override
    public ScriptEngineFactory getFactory() {
        return factory;
    }
}
